package com.netease.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.netease.domain.RepositoryItem;
import com.netease.service.AccessTokenService;
import com.sun.tools.javac.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongle on 2017/2/23.
 */
@Component
public class NetServiceCommon {
    @Autowired
    private AccessTokenService accessTokenService;

    public <T> List<T> getList(String listUrl, Class<T> aclass)  throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());
        String response = HttpUtil.getInstance().commonGet(listUrl, header);
        ObjectMapper mapper = new ObjectMapper();
        List<T> ret = mapper.readValue(response, TypeFactory.defaultInstance().constructCollectionType(List.class, aclass));
        return ret;
    }

    public <T> Pair<Integer, List<T>>  getListWithTotal(String listUrl, Class<T> aclass, String listFieldName)  throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());
        String response = HttpUtil.getInstance().commonGet(listUrl, header);

        JacksonJsonParser parser = new JacksonJsonParser();
        Map<String, Object> jsonMap = parser.parseMap(response);
        Integer total = (Integer) jsonMap.get("total");
        List list = (List) jsonMap.get(listFieldName);

        ObjectMapper mapper = new ObjectMapper();
        List<T> listT = mapper.convertValue(list, TypeFactory.defaultInstance().constructCollectionType(List.class, aclass));
//        List<T> ret = mapper.readValue(response, TypeFactory.defaultInstance().constructCollectionType(List.class, aclass));
        return new Pair<>(total, listT);
    }

    public <T> List<T>  getListAll(String listUrl, Class<T> aclass, String listFieldName, String pageParamName, String pageSizeParamName)  throws IOException {
        int page = 0;
        int pageSize = 20;
        int curCount = 0;
        Integer total;
        ArrayList<T> resultList = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        do {
            String url;
            if (listUrl.contains("?")){
                url = String.format("%s&%s=%d&%s=%d", listUrl, pageParamName, page, pageSizeParamName, pageSize);
            }
            else{
                url = String.format("%s?%s=%d&%s=%d", listUrl, pageParamName, page, pageSizeParamName, pageSize);
            }
            Map<String, String> header = new HashMap<>();
            header.put("Authorization", "Token "+accessTokenService.getToken());

            String response = HttpUtil.getInstance().commonGet(url, header);

            JacksonJsonParser parser = new JacksonJsonParser();
            Map<String, Object> jsonMap = parser.parseMap(response);
            total = (Integer) jsonMap.get("total");
            List list = (List) jsonMap.get(listFieldName);
            curCount += list.size();
            page = curCount;

            List<T> listT = mapper.convertValue(list, TypeFactory.defaultInstance().constructCollectionType(List.class, aclass));
            resultList.addAll(listT);
        } while (curCount < total);

        return resultList;
    }


    public <T> T getResponse(String url, Class<T> aclass)  throws IOException {
        Map<String, String> header = new HashMap<>();
        header.put("Authorization", "Token "+accessTokenService.getToken());
        String response = HttpUtil.getInstance().commonGet(url, header);
        ObjectMapper mapper = new ObjectMapper();
        T ret = mapper.readValue(response, aclass);
        return ret;
    }

}
